package com.gpb.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URI;

/**
 * 卡bin 工具
 * @author 郭彭彬
 *
 */
public class CardBinUtils {
	private File card_bin_file=null;
	private static String filename="card_bin.bin";
	private static CardBinUtils cardBinUtils=new CardBinUtils();
	private CardBinUtils(){
		//setCardBinFile(CardBinUtils.class.getProtectionDomain()
		//		.getCodeSource().getLocation().toString().substring(6),filename);
		//setCardBinFile(CardBinUtils.class.getResource("/").toString().substring(6),filename);

		setCardBinFile(webRoot()+"WEB-INF/classes/", filename);
		
	}
	/**
	 * 获取项目物理路径
	 * @return
	 */
	private static String webRoot(){
		String path=null;
		try {
			URI uri = new URI(WebUtils.class.getResource("/").toString());
			path=uri.getPath().substring(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int endIndex=path.indexOf("WEB-INF/classes/");
		return path.substring(0,endIndex);
	}
	private void setCardBinFile(String path,String filename){
		File[] files=new File(path).listFiles();
		if(files!=null)
		for (File file : files) {
			if(file.isDirectory()){
				setCardBinFile(file.getPath(),filename);
			}
			if(file.isFile()){
				System.out.println(file.getName());
				if(file.getName().equalsIgnoreCase(filename)){
					this.card_bin_file=file;
					break;
				}
			}
		}
	}
	/**
	 * 获取单例
	 * @return
	 */
	public static CardBinUtils newInstance(){
		if(cardBinUtils==null){
			cardBinUtils=new CardBinUtils();
		}
		return cardBinUtils;
	}
	
	public BufferedReader getBufferedReader(){
		try {
			return new BufferedReader(new FileReader(card_bin_file));
		} catch (Exception e) {
			System.out.println(filename+"   文件没找到");
			e.printStackTrace();
		}
		return null;
	}

	
	/**
	 * @param card
	 * @return new String[]{"0:机构名称","1:机构编码","2:卡bin名称","3:卡长度","4:bin长度","5:cardbin","6:卡类型"}
	 */
	public String[] getBin(String card){
		try {
			
			checkCard(card);
			BufferedReader bar=CardBinUtils.newInstance().getBufferedReader();
			String srr=null;
			String[] strs=null;
			while((srr=bar.readLine())!=null){
				strs=srr.split(",");
				if(strs[5].equalsIgnoreCase(card.substring(0,Integer.parseInt(strs[4])))){
					return strs;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		if(card!=null)
			card="";
		return new String[]{"未知银行","0","磁条卡号",""+card.length(),""+card.length(),card,"1"};
	}
	
	public static void checkCard(String card){
		if (card.length() < 16 || card.length() > 22) {
			throw new RuntimeException("无效卡号，非银行卡 :"+card);
		}
	}
	
	/**
	 * 获取归属行和卡类型名称
	 * @param card
	 * @return
	 */
	public String getBank(String card){
		String[] str=getBin(card);
		return str[0]+"."+str[2];
	}
	
	/**
	 * 获取卡类型
	 * @param card
	 * @return
	 */
	public String getCardType(String card){
		String[] str=getBin(card);
		return "1".equalsIgnoreCase(str[6])?"储蓄卡":"2".equalsIgnoreCase(str[6])?"信用卡":"3".equalsIgnoreCase(str[6])?"预付费卡":"";
	}
	
	/**
	 * 获取机构编号
	 * @param card
	 * @return
	 */
	public String getOrgCode(String card){
		String[] str=getBin(card);
		return str[1];
	}
	
	
	public static void main(String[] args) {
		try {
			System.out.println(CardBinUtils.newInstance().getOrgCode("6227001822590148895"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		Properties p=System.getProperties();
//		Iterator<Object> iterator=p.keySet().iterator();
//		while(iterator.hasNext()){
//			Object key=iterator.next();
//			System.out.println(key+"  "+p.get(key));
//		}
		
	}
}
